package com.sc.VenusQueryBeta.presenter.account.daikuan;


import com.hengjun.kingstar.sdk.utils.LogUtils;
import com.sc.VenusQueryBeta.contract.account.daikuan.DaiKuan01Contract;
import com.sc.VenusQueryBeta.model.account.daikuan.DaiKuanModel01;
import com.sc.VenusQueryBeta.model.bean.account.CurrencyListBean;
import com.sc.VenusQueryBeta.model.bean.account.daikuan.DaiKuanBean01;

import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2018/10/18.
 */

public class DaiKuan01Presenter extends DaiKuan01Contract.DaiKuanPresenter {
    private int mCount = 10;
    private int mStart;
    private boolean isLoading;

    @Override
    protected DaiKuan01Contract.IDaiKuan01Model getModel() {
        return DaiKuanModel01.newInstance();
    }

    public static DaiKuan01Presenter newInstance() {
        return new DaiKuan01Presenter();
    }
    @Override
    public void onStart() {

    }
    /**
     * 获取存卡01
     * @param serverAddress
     * @param projectName
     * @param serverPort
     * @param clientindex
     * @param clientAppmsgnum
     */
    @Override
    public void getDaiKuan01(String serverAddress, String projectName, int serverPort, int clientindex, String clientAppmsgnum,String currencynameCN) {
        if (mIView == null || mIModel == null)
            return;
        mStart = 0;
        mRxManager.register(mIModel.getDaiKuan01(serverAddress,projectName, serverPort,clientindex,mStart,mCount,clientAppmsgnum,currencynameCN).subscribe(new Consumer<DaiKuanBean01>() {
            @Override
            public void accept(DaiKuanBean01 daiKuanBean01) throws Exception {
                if (mIView != null)
                    mStart += mCount;
                mIView.showDaikuanData1Success(daiKuanBean01);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView != null) {
//                    if (mIView.isVisiable())
                    LogUtils.e("第一次加載 error=="+throwable.getMessage());
                    mIView.showToast("Network error");
                    mIView.showNetworkError(throwable);
                }
            }
        }));
    }

    /**
     *  获取更多存卡01
     * @param serverAddress
     * @param projectName
     * @param serverPort
     * @param clientindex
     * @param clientAppmsgnum
     */
    @Override
    public void getMoreDaiKuan01(String serverAddress, String projectName, int serverPort, int clientindex, String clientAppmsgnum,String currencynameCN) {
        if (!isLoading) {
            isLoading = true;
            //一次加载 条数据
            mRxManager.register(mIModel.getDaiKuan01(serverAddress,projectName, serverPort,clientindex,mStart,mCount,clientAppmsgnum,currencynameCN).subscribe(new Consumer<DaiKuanBean01>() {
                @Override
                public void accept(DaiKuanBean01 daiKuanBean01) throws Exception {
                    isLoading = false;
                    if (mIView == null)
                        return;
                    if (daiKuanBean01 != null && daiKuanBean01.getList() != null && daiKuanBean01.getList().size() > 0) {
                        mStart += mCount;
                        LogUtils.e("mStart1==" + mStart);
                        mIView.showDaikuanData1Success(daiKuanBean01);
                        //缓存数据

                    } else {
                        //否则显示没有加载更多数据
                        mIView.showNoMoreData();
                    }

                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    isLoading = false;
                    if (mIView != null) {
//                    if (mIView.isVisiable())
                        LogUtils.e("加載更多 error=="+throwable.getMessage());
                        mIView.showToast("Network error");
                        mIView.showNetworkError(throwable);
                    }
                }
            }));
        }
    }



    @Override
    public void queryAllCurrencyList(String serverAddress, String projectName, int serverPort) {
        if (mRxManager != null&&mIModel!=null) {
            mRxManager.register(mIModel.queryAllCurrencyList(serverAddress, projectName, serverPort).subscribe(new Consumer<CurrencyListBean>() {
                @Override
                public void accept(CurrencyListBean currencyListBean) throws Exception {
                    mIView.showqueryAllCurrencyList(currencyListBean);
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    LogUtils.e("Network error." + throwable.getMessage());
//                if (mIView != null)
//                    return;
                    mIView.showToast("Network error.");
                    mIView.showNetworkError(throwable);
                }
            }));
        }else {

        }
    }

}
